// Leader/invite/deputy-apply/deputy-apply.js
/**
 * @params type: 0-团长邀请别人 1-副团长接受别人
 */
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		type: 0,
		title: "",
		ratio: "",
		userInfo: {},
		addressVo: "",
		buttonStatus: "",
		addressId: 0,
	},
	onShare(){
		if(this.data.addressVo === ''){
			wx.showToast({
				icon:'none',
			  	title: '请到团长首页选择提货点',
			})
			return
		}
		if(this.data.ratio === ''){
			wx.showToast({
				icon:'none',
			  	title: '请输入佣金比例',
			})
			return
		}
		this.setData({
			buttonStatus:'share'
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		let { ratio, leaderId, addressVo, addressId } = options;
		const userInfo = wx.getStorageSync('userInfo') || {}
		let type = 0 ;
		console.log(addressVo)
		if(leaderId){
			if(leaderId == userInfo.id){
				// 自己打开
				type = 0;
				ratio = ""
			}else{
				// 别人打开
				type = 1;
				ratio = ratio ? ratio  : ""
			}
		}else{
			// 不是分享打开的
			ratio = ""
		}
		addressVo = addressVo ? addressVo : wx.getStorageSync("addressVo").storeName;
		addressId = addressId ? addressId : wx.getStorageSync("addressVo").id;
		// const type = 0 ;
		this.setData({
			type: type,
			title: type == 0 ? '邀请副团长' : '副团长接受邀请',
			userInfo: userInfo,
			ratio: ratio,
			leaderId: leaderId || 0,
			addressVo: addressVo,
			addressId: addressId
		})
		wx.setNavigationBarTitle({
			title: this.data.title
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		let userInfo = wx.getStorageSync('userInfo');
		let token = wx.getStorageSync('token');
		// 页面显示
		if (userInfo && token) {
			app.globalData.userInfo = userInfo;
			app.globalData.token = token;
		}
		this.setData({
			userInfo: app.globalData.userInfo,
		});
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {
		let { ratio, addressVo,userInfo, addressId } = this.data;
		return {
			title: '邀请你成为我的副团长～',
			imageUrl: '', // 图片 URL
			path: "/Leader/invite/deputy-apply/deputy-apply?ratio="+ratio+"&leaderId="+userInfo.id + "&addressVo="+addressVo + "&addressId=" + addressId
		}
	},
	ratioInput(e){
		this.setData({
			ratio: e.detail.value
		})
		if(this.data.ratio === ''){
			this.setData({
				buttonStatus:''
			})
		} else {
			this.setData({
				buttonStatus:'share'
			})
		}
	},
	invitationLeaderUrl(){
		if(app.globalData.userInfo && app.globalData.userInfo.phone){
			const addressId = wx.getStorageSync("addressId") || 0;
			let { leaderId, ratio, userInfo } = this.data;
			wx.showLoading({
				title: "正在加载",
			});
			util.request(api.leader.invitationLeaderUrl, {
				addressId,
				commission: ratio,
				leaderId,
				status: 1, //状态( 1：启用 0：禁用）
				workerMemberId: userInfo.id
			}, "POST").then(res=>{
				console.log("res", res);
				wx.showToast({
					title: "接受成功"
				})
				wx.hideLoading();
				setTimeout(()=>{
					wx.reLaunch({
						url: '/pages/ucenter/index/index?from=apply'
					})
				},1500);
			}).catch(err=>{
				wx.showToast({
					title: err.message,
					icon: "none"
				})
			})
			
			return ;
		}
		this.goLogin();
	},
	goLogin() {
        if(app.globalData.userInfo && app.globalData.userInfo.phone){
            return false
        }
        wx.navigateTo({ 
            url: '/pages/authorized/index'
        })
    },
})